package com.example.hcd.menuactivity;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.hcd.R;
import com.example.hcd.adapt.ViewUtils;
import com.example.hcd.datamanage.MenuManage;
import com.example.hcd.functionactivity.BaseActivity;
import com.example.hcd.functionactivity.BorrowApply;
import com.example.hcd.functionactivity.BorrowListActivity;
import com.example.hcd.functionactivity.MyTaskList;
import com.example.hcd.internet.LinkHelp;
import com.example.hcd.internet.URLS;
import com.example.hcd.tool.MyLoadingDialog;
import com.example.hcd.tool.ToastHelp;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class BorrowManageMenuActivity extends BaseActivity {

	private BorrowManageMenuActivity borrowManageMenuActivity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other_menu_view);
		mPageName = "BorrowManageMenuActivity";
		borrowManageMenuActivity = this;

		ListView listView = (ListView) findViewById(R.id.list);
		
		TextView title = (TextView)findViewById(R.id.title_content);
		title.setText(this.getString(R.string.borrow_apply));
		findViewById(R.id.title_bar_btn_left).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				finish();
			}
		});

		ViewUtils view1 = new ViewUtils(this, R.layout.other_view_item);

		view1.setList(MenuManage.getInstence().getMainMenu(this, MenuManage.getInstence().menu6Id));
		listView.setAdapter(view1);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				ListView listView = (ListView) parent;
				switch (position) {
				case 1:
					Intent intent = new Intent(borrowManageMenuActivity, BorrowListActivity.class);
					intent.putExtra("TASK_TYPE","my_borrow_list");
					startActivity(intent);
					
					break;

				case 3: {
					new IsCanApply().execute();
//					Toast.makeText(borrowManageMenuActivity, "新建借款申请", Toast.LENGTH_SHORT).show();
				}

					break;

				case 2:
					Intent intent2 = new Intent(borrowManageMenuActivity, MyTaskList.class);
					intent2.putExtra("TASK_TYPE","all_borrow_todo");
					startActivity(intent2);
					break;
					
				case 0:
					Intent intent3 = new Intent(borrowManageMenuActivity, BorrowListActivity.class);
					intent3.putExtra("TASK_TYPE","all_borrow_list");
					startActivity(intent3);
					break;

				default:
					break;
				}
			}
		});

	}
	
	
	// 获取是否有在执行中的申请
		private class IsCanApply extends AsyncTask<String, Integer, String> {

			// private ProgressDialog mProgress;
			MyLoadingDialog dialog;
			public IsCanApply() {
			}

			@Override
			protected void onPreExecute() {
				dialog = new MyLoadingDialog(BorrowManageMenuActivity.this, null, "获取是否可申请信息...", false);
				super.onPreExecute();
			}

			@Override
			protected String doInBackground(String... params) {
				String result = null;
				HttpResponse response = null;
				BufferedReader reader = null;
				try {
					response = LinkHelp.post(URLS.getIsCanApply(), null, false);

					if (response != null) {
						StatusLine line = response.getStatusLine();
						if (line.getStatusCode() == 200) {
							reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
							StringBuilder sb = new StringBuilder();
							for (String s = reader.readLine(); s != null; s = reader.readLine()) {
								sb.append(s);
							}
							reader.close();
							result = sb.toString();

							System.out.println(result);
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return result;
			}

			@Override
			protected void onPostExecute(String result) {
				dialog.dimiss();
				if(result==null)
				{
					ToastHelp.doToast(borrowManageMenuActivity, "网络异常！");
					return;
				}
				
				JSONObject jsonObject;
				try {
					jsonObject = new JSONObject(result);
					if (jsonObject.isNull("hasRunningBorrow")) {
						
					}else
					{
						boolean iscan = jsonObject.getBoolean("hasRunningBorrow");
						if(!iscan)
						{
							Intent intent1 = new Intent(borrowManageMenuActivity, BorrowApply.class);
							startActivity(intent1);
						}else
						{
							ToastHelp.doToast(borrowManageMenuActivity, "当前有未完成的借款申请！请结束后再申请");
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

		}
	

}